<?php
/**
 * Created by Pako
 * Date: 2019/2/24
 * Time: 14:20
 */

namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Lib\Algorithm\Algorithm;

class AlgorithmController extends Controller
{
    public function index()
    {
        $alg = new Algorithm();
        //$data = $alg->bucketSort([3, 1, 7, 4, 9, 5, 3], 10);
        //$data = $alg->bubbleSort([3, 1, 7, 4, 9, 5, 3]);
        //$data = $alg->swapSort([3, 1, 7, 4, 9, 5, 3]);
        $a = [8, 1, 7, 4, 9, 5, 3];
        $data = $alg->selectSort($a);

        print_r($data);
    }

    public function selectSort($arr)
    {
        $len = count($arr);

        for ($i = 0; $i < $len; $i++) {
            $p = $i;
            for ($j = $i + 1; $j < $len; $j++) {
                if ($arr[$p] > $arr[$j]) {
                    $p = $j;
                }
            }

            if ($p != $i) {
                $temp = $arr[$p];
                $arr[$p] = $arr[$i];
                $arr[$i] = $temp;
            }
        }
        return $arr;
    }
}